package cn.ainit.fx.pixiao;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ReturnObject;
import cn.ainit.fx.pixiao.entity.FxPiXiao;
import cn.ainit.fx.pixiao.entity.FxPiXiaoItem;
import cn.ainit.fx.pixiao.entity.PiXiaoObj;
import cn.ainit.sys.utils.UUID32;

@Controller
@RequestMapping("/pixiao")
public class FxPiXiaoController {
	private FxPiXiaoService fxPiXiaoService;
	@Resource
	public void setFxPiXiaoService(FxPiXiaoService fxPiXiaoService) {
		this.fxPiXiaoService = fxPiXiaoService;
	}
	
	@RequestMapping("/savePiXiao")
	@ResponseBody
	public ReturnObject savePiXiao(@RequestBody PiXiaoObj piXiaoObj, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		FxPiXiao fxPiXiao = piXiaoObj.getFxPiXiao();
		List<FxPiXiaoItem> faXiaoItems = piXiaoObj.getFxPiXiaoItems();

		try {
			if (fxPiXiao.getPxId() != null && !fxPiXiao.getPxId().equals("")) {
				//删除旧的不存在的记录    条件  px_id 和px_item_id
				if(fxPiXiaoService.updateSprk(fxPiXiao,faXiaoItems)){
					returnObject.setMsg("upsuccess");
				}else{
					returnObject.setMsg("upfail");
				}
				
			}else{
				String pxId = UUID32.newGuid();
				fxPiXiao.setPxId(pxId);
				//保存操作
				if(fxPiXiaoService.saveSprk(fxPiXiao,faXiaoItems)){
					returnObject.setMsg("success");
					returnObject.setInfo(pxId);
				}else{
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			returnObject.setMsg(e.getMessage());
			e.printStackTrace();
		}
	    return	returnObject;
	}
	/**
	 * 批销单批退单出 入库出库
	 * @param pxId
	 * @param stockMan
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-17 上午11:19:01
	 */
	@RequestMapping("/piXiaoCRK")
	@ResponseBody
	public ReturnObject piXiaoCRK(String pxId,String stockMan,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String msg = this.fxPiXiaoService.execPiXiaoRuku(pxId, stockMan);
			returnObject.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 批销单批退单的承收
	 * @param pxId
	 * @param stockMan
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-22 上午10:27:15
	 */
	@RequestMapping("/chengShou")
	@ResponseBody
	public ReturnObject chengShou(String pxId,String finMan,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String msg = this.fxPiXiaoService.execChengShou(pxId, finMan);
			returnObject.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
}
